/*
 * @Author: Leng
 * @Date: 2022-11-07 15:50:42
 * @Description: config初始化
 * @FilePath: \vue-leng\src\config\init.js
 */

// 公共工具初始化
// 原生对象拓展，兼容低版本浏览器语法
import './prototype_expand'
// 跳转
import './jump'
// 工具类
import './tool'
// 时间处理工具
import moment from 'moment'
// 工具函数
import 'lodash'
import Map from './map/index'
import WebMap from './map/map.web'
// 解决ios因软键盘弹出导致元素偏移的问题
function fixInputLayout() {
  const isIphone = !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
  let keybordShow = false
  document.body.addEventListener('focusin', (e) => {
    keybordShow = true
    setTimeout(function () {
      fixLayout(e)
    }, 520)
  })
  document.body.addEventListener('focusout', () => {
    keybordShow = false
    setTimeout(function () {
      fixLayout()
    }, 100)
  })
  function fixLayout(e) {
    if (isIphone) {
      if (!keybordShow) {
        window.scroll(document.body.scrollHeight, 0)
        // document.body.scrollTop = document.body.scrollHeight
        // document.body.scrollTop = 0;
      }
    } else {
      if (e && e.target.nodeName == 'INPUT') {
        keybordShow && e.target.scrollIntoView(false)
      }
    }
  }
}

// vivo app未点击隐私条款时,不能获取用户设备信息; 百度地图, 极光推送可能会调用手机权限
 export function lazyInit(){
  console.log('lazyInit.....')
  if(typeof(window.isLazyInit)!=='undefined' && window.isLazyInit === true){
    console.log('lazyInited....')
    return 
  }

  if (window.api) {
    if (window.api.systemType == 'ios') {
      let aliPush = window.api.require('aliPush');
      if (aliPush && aliPush.registerPush) {
        aliPush.registerPush(function(ret) {
          console.log(JSON.stringify(ret));
        });
      }
    }
    // 初始化app-common项目中的云客服
    window.api.sendEvent({name: 'initAlipush', extra: {} })
  }
  window.isLazyInit = true
}

export function lazyInitMap() {
  console.log('lazyInitMap.....',localStorage.getItem('MAP_POSITIONING'))
  if(typeof(window.isLazyInitMap)!=='undefined' && window.isLazyInitMap ===true){
    console.log('lazyInitMap  inited....')
    return 
  }
  if('H5' === localStorage.getItem('MAP_POSITIONING') ) {
    console.log('强制地图使用h5')
    $tool.map = new WebMap()
  }else{
    console.log('地图自动适配')
    $tool.map = Map()
  }

  $tool.webMap = new WebMap()
  window.isLazyInitMap = true
}
export default async function () {
  // window._ = _
  moment.locale('zh-cn')
  window.moment = moment
  //公共组件图标
  // $tool.loadScript('//at.alicdn.com/t/font_1267007_sm35l0xo8f.js', null, 'wxm-font-js')
  // $tool.loadCSS('//at.alicdn.com/t/font_1267007_sm35l0xo8f.css', 'wxm-font-css')
  let iconUrl = $config.iconfont
  $tool.loadCSS(iconUrl + '.css', null, 'leng-font-css')
  $tool.loadScript(iconUrl + '.js', null, 'leng-font-js')
  console.log('加载iconfont:' + iconUrl)
  // 非pc模式不记载app工具js
  if (!$tool.isPC()) {
    fixInputLayout()
  }
  //监听浏览器回退事件,只能监听一次
  // window.addEventListener('popstate', function (e) {
  //   if (location.pathname == '/common/pay') {
  //     window.$ZCommonPayCallback && window.$ZCommonPayCallback()
  //   }
  // }, false);
  window.lazyInit = lazyInit
  window.lazyInitMap = lazyInitMap
  // // 挂载全局定位
  // if(localStorage.getItem('forceH5map') && localStorage.getItem('forceH5map') === '1') {
  //   $tool.map = new WebMap()
  // }else{
  //   $tool.map = Map()
  // }
 
  // $tool.webMap = new WebMap()
}
